﻿using Framework.SqlSugarCore.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Uow;

namespace Framework.SqlSugarCore.Uow
{
	/// <summary>
	/// Sugar事务API
	/// </summary>
	public class SqlSugarTransactionApi : ITransactionApi, ISupportsRollback
	{
		private ISqlSugarDbContext _sqlsugarDbContext;

		public SqlSugarTransactionApi(ISqlSugarDbContext sqlsugarDbContext)
		{
			_sqlsugarDbContext = sqlsugarDbContext;
		}

		public ISqlSugarDbContext GetDbContext()
		{

			return _sqlsugarDbContext;
		}

		public async Task CommitAsync(CancellationToken cancellationToken = default)
		{
			await _sqlsugarDbContext.SqlSugarClient.Ado.CommitTranAsync();
		}

		public void Dispose()
		{
			_sqlsugarDbContext.SqlSugarClient.Ado.Dispose();
		}

		public async Task RollbackAsync(CancellationToken cancellationToken = default)
		{
			await _sqlsugarDbContext.SqlSugarClient.Ado.RollbackTranAsync();
		}
	}
}
